package cn.kgc.web.service;

import cn.kgc.common.entity.Article;
import cn.kgc.common.entity.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by teacher ZHANG on 2023/2/6
 */
@Service
public class CountServiceImpl implements CountService {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${rest.count.url}")
    private String countUrl;

    @Override
    public List<Map<String, Object>> listTopCounts(Integer number) {
        Map[] maps = restTemplate.getForObject(countUrl + "?topNum=" + number, Map[].class);
        return Arrays.asList(maps);
    }

    @Override
    public List<Map<String, Object>> listByArticleIds(List<Integer> idList) {
        StringBuilder builder = new StringBuilder();

        for (Integer articleId : idList) {
            if (builder.length() > 0) {
                builder.append(",");
            }

            builder.append(articleId);
        }

        Map[] maps = restTemplate.getForObject(countUrl + "?articleIds=" + builder.toString(), Map[].class);

        return Arrays.asList(maps);
    }


    @Override
    public Integer addAddress(Integer articleId, String address) {
        String url = countUrl + "?articleId=" + articleId + "&address=" + address;

        return restTemplate.postForObject(url, null, Integer.class);
    }
}
